using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace PhoneInput
{
    public enum TouchStickButton
    {
        Up,
        Down,
        Left,
        Right,
        Fire,
        Eye,
        None
    }
    
    public class TouchStick
    {
        public static TouchStick state = new TouchStick(Vector2.Zero, 0.0f, 0.0f, 0.0f);

        public Vector2 padPosition;
        public Vector2 fireButtonPosition;
        public Vector2 eyeButtonPosition;

        Vector2 fireButtonOrigin;
        Vector2 eyeButtonOrigin;
       
        public float padSize;
        public float fireSize;

        const float upRot = -3 * MathHelper.PiOver4;
        const float downRot = MathHelper.PiOver4;
        const float leftRot = 3 * MathHelper.PiOver4;
        const float rightRot = -MathHelper.PiOver4;

        float cosPiOver4 = (float)System.Math.Sqrt(2.0d) * 0.5f;

        float upBorder, downBorder, leftBorder, rightBorder;

        float upFireBorder, downFireBorder, leftFireBorder, rightFireBorder;

        float upEyeBorder, downEyeBorder, leftEyeBorder, rightEyeBorder;

        Color upColor, downColor, leftColor, rightColor;

        TouchCollection touchCollection;

        public TouchStick(Vector2 position, float padSize, float fireSize, float eyeSize)
        {
            padPosition = position;
            fireButtonPosition = position + new Vector2(500, 0);
            eyeButtonPosition = position + new Vector2(550, -140);

            fireButtonOrigin = new Vector2(fireSize * 0.5f, fireSize * 0.5f);
            eyeButtonOrigin = new Vector2(eyeSize * 0.5f, eyeSize * 0.5f);
            
            this.padSize = padSize;
            this.fireSize = fireSize;

            upBorder = padPosition.Y - padSize;
            downBorder = padPosition.Y + padSize;
            leftBorder = padPosition.X - padSize;
            rightBorder = padPosition.X + padSize;

            upFireBorder = fireButtonPosition.Y - (fireSize * 0.5f);
            downFireBorder = fireButtonPosition.Y + (fireSize * 0.5f);
            leftFireBorder = fireButtonPosition.X - (fireSize * 0.5f);
            rightFireBorder = fireButtonPosition.X + (fireSize * 0.5f);

            upEyeBorder = eyeButtonPosition.Y - (eyeSize * 0.5f);
            downEyeBorder = eyeButtonPosition.Y + (eyeSize * 0.5f);
            leftEyeBorder = eyeButtonPosition.X - (eyeSize * 0.5f);
            rightEyeBorder = eyeButtonPosition.X + (eyeSize * 0.5f);


            upColor = downColor = leftColor = rightColor = Color.White;
        }

        public void Draw(SpriteBatch spriteBatch, Texture2D padTexture, Texture2D fireTexture, Texture2D eyeTexture)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(padTexture, padPosition, null, upColor, upRot, Vector2.Zero, 1.0f, SpriteEffects.None, 0.0f);
            spriteBatch.Draw(padTexture, padPosition, null, downColor, downRot, Vector2.Zero, 1.0f, SpriteEffects.None, 0.0f);
            spriteBatch.Draw(padTexture, padPosition, null, leftColor, leftRot, Vector2.Zero, 1.0f, SpriteEffects.None, 0.0f);
            spriteBatch.Draw(padTexture, padPosition, null, rightColor, rightRot, Vector2.Zero, 1.0f, SpriteEffects.None, 0.0f);
            spriteBatch.Draw(fireTexture, fireButtonPosition, null, Color.White, 0.0f, fireButtonOrigin, 1.0f, SpriteEffects.None, 0.0f);
            spriteBatch.Draw(eyeTexture, eyeButtonPosition, null, Color.White, 0.0f, eyeButtonOrigin, 1.0f, SpriteEffects.None, 0.0f);
            spriteBatch.End();
        }

        public TouchStickButton[] getPressedButton()
        {
            upColor = downColor = leftColor = rightColor = Color.White;

            TouchStickButton[] buttons = { TouchStickButton.None, TouchStickButton.None };

            touchCollection = TouchPanel.GetState();
            
            foreach (TouchLocation location in touchCollection)
            {
                if (location.State == (TouchLocationState.Moved | TouchLocationState.Pressed))
                {
                    if (CatchedPad(location.Position))
                    {
                        Vector2 direction = (location.Position - padPosition);
                        direction.Normalize();

                        float cos = Vector2.Dot(direction,Vector2.UnitX);

                        if (cos >= cosPiOver4)
                        {
                            rightColor = Color.CornflowerBlue;
                            buttons[0] = TouchStickButton.Right;
                        }

                        else if (cos <= -cosPiOver4)
                        {
                            leftColor = Color.CornflowerBlue;
                            buttons[0] = TouchStickButton.Left;
                        }

                        else if (direction.Y > 0)
                        {
                            downColor = Color.CornflowerBlue;
                            buttons[0] = TouchStickButton.Down;
                        }
                        else
                        {
                            upColor = Color.CornflowerBlue;
                            buttons[0] = TouchStickButton.Up;
                        }
                       
                    }

                    if (CatchedFire(location.Position))
                    {
                        buttons[1] = TouchStickButton.Fire;
                    }

                    if (CatchedEye(location.Position))
                    {
                        buttons[1] = TouchStickButton.Eye;
                    }
                }

            }

            return buttons;
        }

        private bool CatchedPad(Vector2 touchPosition)
        {
            if ((touchPosition.X >= leftBorder) && (touchPosition.X <= rightBorder))
            {
                if ((touchPosition.Y >= upBorder) && (touchPosition.Y <= downBorder))
                {
                    return true;
                }
            }

            return false;
        }

        private bool CatchedFire(Vector2 touchPosition)
        {
            if ((touchPosition.X >= leftFireBorder) && (touchPosition.X <= rightFireBorder))
            {
                if ((touchPosition.Y >= upFireBorder) && (touchPosition.Y <= downFireBorder))
                {
                    return true;
                }
            }

            return false;
        }

        private bool CatchedEye(Vector2 touchPosition)
        {
            if ((touchPosition.X >= leftEyeBorder) && (touchPosition.X <= rightEyeBorder))
            {
                if ((touchPosition.Y >= upEyeBorder) && (touchPosition.Y <= downEyeBorder))
                {
                    return true;
                }
            }

            return false;
        }


    }
}